//
//  SupplierViewController.m
//  SafeFoodManagerDemo
//
//  Created by ios-1 on 2018/3/14.
//  Copyright © 2018年 bob. All rights reserved.
//

#import "SupplierViewController.h"
#import "HelpListTableViewCell.h"
#import "GetRegisterInforModel.h"
#import "AddRecruitViewController.h"
#import "HelpDetailViewController.h"
#import "RegisterShopModel.h"
#import "SupplyListModel.h"
#import "SupplierOrderShopViewController.h"
#import "SafeFoodManagerDemo-Swift.h"
@interface SupplierViewController ()
@property(nonatomic,copy)NSString*SearchKeyWord;
@property(nonatomic,copy)NSString*positionId;      //当前岗位ID
@property(nonatomic,assign)BOOL is_selectPosition; //是否点击当前岗位

@property(nonatomic,strong)RegisterShopModel *ShopModel;
@end

@implementation SupplierViewController

- (void)viewDidLoad {
    
  
    [super viewDidLoad];
    
    self.homeTableView.estimatedRowHeight=70;
    
    [self.homeTableView registerNib:[UINib nibWithNibName:@"HelpListTableViewCell" bundle:nil] forCellReuseIdentifier:@"HelpCell"];
    [self.homeTableView.mj_header beginRefreshing];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    DLog("控制器出现++++++");
    
    
}

#pragma mark-tablevie代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SupplyData *data=self.dataArr[indexPath.row];
    
    HelpListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"HelpCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = data.supplierName;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SupplyData *data=self.dataArr[indexPath.row];
    
    SupplierOrderShopViewController *Patro=[SupplierOrderShopViewController new];
    OrderReqModel *list=[OrderReqModel new];
    list.pageOn = YES;
    list.pageSize = 50;
    list.currentPage = 1;
    list.shopId = [NSString stringWithFormat:@"%zd",[LoginStatuesModel shopId]];
    
    Patro.supplierId = data.supplierId;
    Patro.ishidenSearch = NO;
    Patro.Reqmodel = list;
    Patro.isEdit= NO;
    Patro.isdeleMore=NO;
    Patro.isGroupStyle=YES;
    Patro.searchName=@"请输入商品名称";
    Patro.title= @"新增订货";
    [self.navigationController pushViewController:Patro animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

-(void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    
    view.tintColor=TBColorTableViewBg;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 0.1;
    
}

#pragma mark-搜索跳转事件
-(void)PushChangeStatueVC
{
    BOOL isSelf=self.Reqmodel.shopId==0?YES:NO;
    
    BaseSearchChooseViewController *Choose=[[BaseSearchChooseViewController alloc]init];
    
    Choose.SearchPlace=self.searchName;
    
    Choose.KeyWord=self.SearchKeyWord;
    
    Choose.Lshopid=self.Reqmodel.shopId;
    
    Choose.SureBlock=^(NSString *paras,NSString*keyword)
    {
        self.Reqmodel.params=paras;
        
        self.SearchKeyWord=keyword;
        
        [self.homeTableView.mj_header beginRefreshing];
    };
    
    [self.navigationController pushViewController:Choose animated:YES];
}

#pragma mark-空白页点击事件
- (void)actionTapEmptyDataSetNodataView
{
    [self.homeTableView.mj_header beginRefreshing];
}

/**
 *  MJ头部刷新(封装层方法, 需要子类继承或者重写)
 */
- (void)actionMJHeaderRefresh
{
    self.Reqmodel.currentPage=1;
    
    [self.homeTableView.mj_footer resetNoMoreData];
    
    [self loadDatas];
}

/**
 *  MJ底部加载(封装方法, 需要子类继承或者重写)
 */
- (void)actionMJFooterLoadMore
{
    self.Reqmodel.currentPage++;
    
    [self loadDatas];
}

-(void)loadDatas
{
    [self showLoadingAnimation];
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/supplierList.json" andrequestTime:5 andParams:self.Reqmodel.mj_keyValues andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        [self stopLoadingAnimation];
        
        SupplyListModel *InforModel=[SupplyListModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
            
            self.TotalNum=[NSString stringWithFormat:@"一共找到%ld个%@",[responseObject[@"totalRecord"]integerValue],@"记录"];
            if (InforModel.data.count==0&&self.Reqmodel.currentPage==1) {
                
                [self.dataArr removeAllObjects];
                
                self.Type=TBNODateType;
                
                [self.homeTableView reloadData];
                
            }else if (InforModel.data.count==0&&self.Reqmodel.currentPage!=1){
                
                self.btn.hidden=NO;
                [self.homeTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else{
                
                self.btn.hidden=NO;
                
                if (self.Reqmodel.currentPage==1) {
                    
                    [self.dataArr removeAllObjects];
                    
                }
                
                [self.dataArr addObjectsFromArray:InforModel.data];
                
                [self.homeTableView reloadData];
                
            }
            
            [self NetRequestSuccess:self scrollView:self.homeTableView];
            
        }else if (responseStatus==0||responseStatus==2)
        {
            if (self.Reqmodel.currentPage>1) {
                
                self.Reqmodel.currentPage=self.Reqmodel.currentPage-1;
            }
            
            [self NetRequestFail:self scrollView:self.homeTableView msg:message];
            
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
        }
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
